/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.odhixon.volarme.gui;

import com.odhixon.volarme.facades.RegistrosFachada;
import com.odhixon.volarme.persistence.RegistroPerst;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.spinner.Spinner;
import java.util.Calendar;
import java.util.Date;

/**
 * RegistroForm.
 * @author Ariel Aguirre <odhixon@gmail.com>
 */
public class RegistroForm extends Form {

    static private RegistroForm instance = null;

    static public RegistroForm getInstance() {
        if (instance == null) {
            instance = new RegistroForm();
        }
        return instance;
    }
    private Form backForm;
    private Label fechaLabel;
    private Spinner diaSpinner;
    private Spinner mesSpinner;
    private Spinner añoSpinner;
    private Label itinerarioLabel;
    private TextField itinerarioTextField;
    private Label duracionLabel;
    private TextField duracionTextField;
    private Spinner duracionSpinner;
    private Label aterrizajesLabel;
    private Spinner aterrizajesSpinner;
    private Command cancelarCommand;
    private Command guardarCommand;
    private RegistroPerst registro;

    public void setBackForm(Form backForm) {
        this.backForm = backForm;
    }

    public void setRegistro(RegistroPerst registro) {
        this.registro = registro;
    }

    public RegistroForm() {
        super();
        //this form.
        this.setTitle("Registro de vuelo");
        this.setScrollable(false);
        this.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        Container container = new Container();
        this.addComponent(container);
        fechaLabel = new Label("Fecha:");
        container.addComponent(fechaLabel);
        diaSpinner = Spinner.create(1, 32, 1, 1);
        container.addComponent(diaSpinner);
        mesSpinner = Spinner.create(1, 13, 1, 1);
        container.addComponent(mesSpinner);
        añoSpinner = Spinner.create(1970, 3000, 2010, 1);
        container.addComponent(añoSpinner);

        container = new Container(new BorderLayout());
        this.addComponent(container);
        itinerarioLabel = new Label("Itinerario:");
        container.addComponent(BorderLayout.WEST, itinerarioLabel);
        itinerarioTextField = new TextField();
        container.addComponent(BorderLayout.CENTER, itinerarioTextField);

        container = new Container();
        this.addComponent(container);
        duracionLabel = new Label("Duracion:");
        container.addComponent(duracionLabel);
        duracionSpinner = Spinner.create(0.0, 100.0, 1.00, 0.1);
        container.addComponent(duracionSpinner);

        container = new Container();
        this.addComponent(container);
        aterrizajesLabel = new Label("Aterrizajes:");
        container.addComponent(aterrizajesLabel);
        aterrizajesSpinner = Spinner.create(0, 1000, 1, 1);
        container.addComponent(aterrizajesSpinner);

        //cancelar command.
        cancelarCommand = new Command("Cancelar") {

            public void actionPerformed(ActionEvent actionEvent) {
                backForm.show();
            }
        };
        this.addCommand(cancelarCommand);
        //guardar command.
        guardarCommand = new Command("Guardar") {

            public void actionPerformed(ActionEvent actionEvent) {
                if (registro == null) {
                    registro = new RegistroPerst();
                    registro.setId(-1);
                }
                Calendar calendar = Calendar.getInstance();
                try {
                    calendar.set(Calendar.DAY_OF_MONTH, ((Integer) diaSpinner.getValue()).intValue());
                    calendar.set(Calendar.MONTH, ((Integer) mesSpinner.getValue()).intValue() - 1);
                    calendar.set(Calendar.YEAR, ((Integer) añoSpinner.getValue()).intValue());
                    registro.setFecha(calendar.getTime().getTime());
                } catch (Exception exception) {
                    Dialog.show("Advertencia", "Fecha incorrecta", "Aceptar", null);
                    return;
                }
                registro.setItinerario(itinerarioTextField.getText());
                registro.setDuracion(((Double) duracionSpinner.getValue()).doubleValue());
                registro.setAterrizajes(((Integer) aterrizajesSpinner.getValue()).intValue());
                RegistrosFachada.getInstance().save(registro);
                backForm.show();
            }
        };
        this.addCommand(guardarCommand);
    }

    public void onShow() {
        if (registro == null) {
            Calendar calendar = Calendar.getInstance();
            diaSpinner.setValue(new Integer(calendar.get(Calendar.DAY_OF_MONTH)));
            mesSpinner.setValue(new Integer(calendar.get(Calendar.MONTH) + 1));
            añoSpinner.setValue(new Integer(calendar.get(Calendar.YEAR)));
            itinerarioTextField.setText("");
            duracionSpinner.setValue(new Double(1.00));
            aterrizajesSpinner.setValue(new Integer(1));
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date(registro.getFecha()));
            diaSpinner.setValue(new Integer(calendar.get(Calendar.DAY_OF_MONTH)));
            mesSpinner.setValue(new Integer(calendar.get(Calendar.MONTH) + 1));
            añoSpinner.setValue(new Integer(calendar.get(Calendar.YEAR)));
            itinerarioTextField.setText(registro.getItinerario());
            duracionSpinner.setValue(new Double(registro.getDuracion()));
            aterrizajesSpinner.setValue(new Integer(registro.getAterrizajes()));
        }
    }
}
